package cn.tom.test;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class WorkdayCalculator {

    public static long calculateWorkdays(LocalDate startDate, LocalDate endDate) {
        // 确保开始日期不晚于结束日期
        if (startDate.isAfter(endDate)) {
            throw new IllegalArgumentException("开始时间不能晚于结束时间");
        }

        long workdays = 0;

        LocalDate date = startDate;
        while (!date.isAfter(endDate)) {
            // 如果是工作日（周一到周五），则计数
            if (isWorkday(date)) {
                workdays++;
            }
            date = date.plusDays(1);
        }

        return workdays;
    }

    // 判断是否为工作日（周一到周五）
    private static boolean isWorkday(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY;
    }

    public static void main(String[] args) {
        /*LocalDate startDate = LocalDate.of(2024, 8, 23);
        LocalDate endDate = LocalDate.of(2024, 9, 5);*/
        String formatDate = DateUtil.formatDate(new Date());
        System.out.println("当前时间" +formatDate);


        String startDateStr = "2024-09-29";
        String endDateStr = "2024-10-03";
        LocalDate startDate = LocalDateTimeUtil.parseDate(startDateStr);
        LocalDate endDate = LocalDateTimeUtil.parseDate(endDateStr);
        long workdays = calculateWorkdays(startDate, endDate);
        System.out.println("工作日天数: " + workdays);
    }
}
